﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace University.Workshop.DataAccess
{
    public class DataAccessCutSupply
    {
        public static void RegisterNewCutSupply(Model.CutSupply cut)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.CutSupply.Add(cut);
            WFPEntities.Instance.SaveChanges();
        }
        
        public static ICollection<Model.CutSupply> GetLastCutSupply(Model.Kardex kardex)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.CutSupply
                    where (c.KardexID == kardex.KardexId) && (c.State != (short)Model.CutSupplyState.Cobrado)
                    select c).ToList();
        }



        public static void ChangeCutSupply(int kardexId)
        {
            WFPEntities.Instance.DiscardChanges();

            Model.CutSupply cs;
            List<Model.CutSupply> cortes = new List<Model.CutSupply>();
            cortes = (from css in WFPEntities.Instance.CutSupply
                      where css.KardexID == kardexId && css.State == 2
                      select css).ToList();

            cs = cortes.First();
            cs.State = 3;

            WFPEntities.Instance.SaveChanges();
        }

        public static bool CheckCutSupply(int kardexId)
        {
            WFPEntities.Instance.DiscardChanges();

            List<Model.CutSupply> cortes = new List<Model.CutSupply>();
            cortes = (from css in WFPEntities.Instance.CutSupply
                      where css.KardexID == kardexId && css.State == 2
                      select css).ToList();

            if (cortes.Count > 0)
                return true;
            else
                return false;
        }

    }
}
